/*
 * @Author: MainStart pxclihai@126.com
 * @Date: 2024-07-30 09:13:32
 * @LastEditors: MainStart pxclihai@126.com
 * @LastEditTime: 2024-08-23 15:18:08
 * @FilePath: \nuohua_eided:\wdjg\project\wzjgwkfty03_mcu\babyos\bos\drivers\b_drv_tm1616_nh.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置:
 * https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/*
 * @Author: MainStart pxclihai@126.com
 * @Date: 2024-07-24 08:37:23
 * @LastEditors: MainStart pxclihai@126.com
 * @LastEditTime: 2024-07-30 13:16:16
 * @FilePath: \edied:\wdjg\project\wzjgwkfty03_mcu\babyos\bos\drivers\b_drv_tm1616.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置:
 * https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/**
 *!
 * \file        b_drv_tm1616.c
 * \version     v0.0.1
 * \date        2024/07/03
 * \author
 *******************************************************************************

 */

/*Includes ----------------------------------------------*/
#include "drivers/inc/b_drv_tm1616_nh.h"

#include <stdbool.h>
#include <string.h>

#include "utils/inc/b_util_log.h"

/**
 * \addtogroup BABYOS
 * \{
 */

/**
 * \addtogroup B_DRIVER
 * \{
 */

/**
 * \addtogroup TM1616
 * \{
 */

/**
 * \defgroup TM1616_Private_Defines
 * \{
 */

#define DRIVER_NAME TM1616

#define MaxSMG 4  // 数码管数目

#define DIGIT4_SEGMENT7 0x00    // 四位七段
#define AUTO_ADD_INCREASE 0x40  // 地址自动增模式
#define FIXED_ADD 0x44          // 地址固定模式

#define DISPLAY_OFF 0xf7  // 显示关
#define DISPLAY_ON 0x08   // 显示关

#define DIG1 0xC0  // 第一个数码管
#define DIG2 0xC2  // 第二个数码管
#define DIG3 0xC4  // 第三个数码管
#define DIG4 0xC6  // 第四个数码管

// 数码管亮度
typedef enum
{
    SMG_Light1 = 0x80,  // 脉冲宽度 1/16
    SMG_Light2,         // 脉冲宽度 2/16
    SMG_Light3,         // 脉冲宽度 4/16
    SMG_Light4,         // 脉冲宽度 10/16
    SMG_Light5,         // 脉冲宽度 11/16
    SMG_Light6,         // 脉冲宽度 12/16
    SMG_Light7,         // 脉冲宽度 13/16
    SMG_Light8          // 脉冲宽度 14/16
} SMG_Light;

#define LED_AUTO_MODE_ICON 0
#define LED_TEMP_UNIT_ICON 1
#define LED_CL_ICON 2
#define LED_HALF_NUM_ICON 3
#define LED_HOT_ICON 4
#define lED_DECIMAPOINT_ICON 5
#define LED_LOW_BATTERY_ICON 6
#define LED_TEMP_UNIT_ICON_R 8
#define LED_HALF_NUM_ICON_R 9
#define lED_DECIMAPOINT_ICON_R 10
#define LED_HOLIDAY_ICON 11
#define LED_ECO_ICON 12
#define LED_WIFI_ICON 13
#define LED_MANUAL_MODE_ICON 14

// 定义LED字母标识符
#define LED_LETTER_A 0
#define LED_LETTER_C 1
#define LED_LETTER_L 2
#define LED_LETTER_O 3
#define LED_LETTER_P 4
#define LED_LETTER_F 5
#define LED_LETTER_D 6
#define LED_LETTER_I 7
#define LED_LETTER_N 8
#define LED_LETTER_E 9
#define LED_LETTER_2 10
#define LED_LETTER_CLOSE 11
#define LED_LETTER_UNKNOWN -1  // 定义未知字母标识符

#define setbit(x, n) ((x) |= (1L << (n)))
#define clrbit(x, n) ((x) &= ~(1L << (n)))
#define isbit(byte, bit) ((byte >> bit) & 0x1)
#define NUM_TO_1(num) (num) % 10
#define NUM_TO_2(num) (num / 10) % 10
#define NUM_TO_3(num) (num / 100)

// BYL百意轮的布局
// static uint8_t BYL_TM1616_ICON[] = {2, 5, 2, 6, 1, 4, 3, 7, 8, 9, 10, 11, 12, 0, 14};

// static uint8_t BYL_TM1616_SMG[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x7,  0x7F,
//                                    0x67, 0x77, 0x7E, 0x39, 0x3F, 0x79, 0x71, 0xF3, 0x38};
// // 倒置
// static uint8_t BYL_TM1616_SMG_R[] = {0x3F, 0x30, 0x5B, 0x79, 0x74, 0x6D, 0x6F, 0x38,
//                                      0x7F, 0x7C, 0x7E, 0x7E, 0x0F, 0x3F, 0x4F, 0x4E};

// static uint8_t BYL_TM1616_LED_LETTER_R[] = {0x7E, 0xF,  0x7,  0x3F, 0x5E, 0xCE,
//                                             0x73, 0x30, 0x3E, 0x4F, 0x5B, 0x00};
// A    C     L    O     P    F
// static uint8_t BYL_TM1616_LED_LETTER[] = {0x77, 0x39, 0x38, 0x3F, 0xF3, 0x71,
//                                           0x5E, 0x6,  0x37, 0x79, 0x5B, 0x00};

static uint8_t TM1616_ICON[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};

static uint8_t TM1616_SMG[] = {0x3F, 0x30, 0x5B, 0x79, 0x74, 0x6D, 0x6F, 0x38,
                               0x7F, 0x7C, 0x7E, 0x7E, 0x0F, 0x3F, 0x4F, 0x4E};
//                     //   0      1    2      3    4     5     6     7      8    9     A     B C D
//                     E     F
// 倒置
static uint8_t TM1616_SMG_R[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x7,  0x7F,
                                 0x67, 0x77, 0x7E, 0x39, 0x3F, 0x79, 0x71, 0xF3, 0x38};
//   0      1    2      3    4     5     6     7      8    9     A     B     C     D     E     F G L

static uint8_t TM1616_LED_LETTER[] = {0x7E, 0xF,  0x7,  0x3F, 0x5E, 0xCE,
                                      0x73, 0x30, 0x3E, 0x4F, 0x5B, 0x00};
// A    C     L    O     P    F
static uint8_t TM1616_LED_LETTER_R[] = {0x77, 0x39, 0x38, 0x3F, 0xF3, 0x71,
                                        0x5E, 0x6,  0x37, 0x79, 0x5B, 0x00};

/**
 * \}
 */

/**
 * \defgroup TM1616_Private_TypesDefinitions
 * \{
 */
typedef struct
{
    uint8_t  type;
    uint16_t num;
    uint8_t  letter1;
    uint8_t  letter2;
    uint16_t icon;
    uint8_t  direction;
    uint8_t *map_smg_data;  // 映射数据
    uint8_t *map_letter_data;
    uint8_t *map_icon;
    uint8_t  brigness;
    bool     is_close;
} bLedScreenPrivate_t;
/**
 * \}
 */

/**
 * \defgroup TM1616_Private_Macros
 * \{
 */

/**
 * \}
 */

/**
 * \defgroup TM1616_Private_Variables
 * \{
 */
bDRIVER_HALIF_TABLE(bTM1616_HalIf_t, DRIVER_NAME);
static bLedScreenPrivate_t led_flush_buffer;
// static bOledPrivate_t bOledPrivateTable[bDRIVER_HALIF_NUM(bTM1616_HalIf_t, DRIVER_NAME)];
/**
 * \}
 */

/**
 * \defgroup TM1616_Private_FunctionPrototypes
 * \{
 */
int bTM1616_Init(bDriverInterface_t *pdrv);
/**
 * \}
 */

/**
 * \defgroup TM1616_Private_Functions
 * \{
 */
static void _bTM1616_SendOneByte(bDriverInterface_t *pdrv, uint8_t mDat)
{
    uint8_t i;
    uint8_t send_data = mDat;
    bDRIVER_GET_HALIF(_if, bTM1616_HalIf_t, pdrv);
    for (i = 0; i < 8; i++)
    {
        bMcuGpioWritePin(_if->clk.port, _if->clk.pin, 0);
        if (send_data & 0x01)
        {
            bMcuGpioWritePin(_if->din.port, _if->din.pin, 1);
        }
        else
        {
            bMcuGpioWritePin(_if->din.port, _if->din.pin, 0);
        }
        bHalDelayUs(10);
        bMcuGpioWritePin(_if->clk.port, _if->clk.pin, 1);
        send_data >>= 1;
    }
}
static void _bTM1616_WriteCmd(bDriverInterface_t *pdrv, uint8_t cmd)
{
    int ret = 0;
    bDRIVER_GET_HALIF(_if, bTM1616_HalIf_t, pdrv);
    bMcuGpioWritePin(_if->stb.port, _if->stb.pin, 0);
    _bTM1616_SendOneByte(pdrv, cmd);
    bMcuGpioWritePin(_if->stb.port, _if->stb.pin, 1);
    (void)ret;
}

static void _bTM1616_WriteData(bDriverInterface_t *pdrv, uint8_t num, uint8_t dat)
{
    int ret = 0;
    bDRIVER_GET_HALIF(_if, bTM1616_HalIf_t, pdrv);
    bMcuGpioWritePin(_if->stb.port, _if->stb.pin, 0);
    _bTM1616_SendOneByte(pdrv, num);
    _bTM1616_SendOneByte(pdrv, dat);
    bMcuGpioWritePin(_if->stb.port, _if->stb.pin, 1);
    (void)ret;
}
static void _led_flush(bDriverInterface_t *pdrv)
{
    bDRIVER_GET_HALIF(_if, bTM1616_HalIf_t, pdrv);

    if (_if->direction == 0)
    {
        clrbit(led_flush_buffer.icon, led_flush_buffer.map_icon[LED_TEMP_UNIT_ICON_R]);
        if (led_flush_buffer.type == 0)
        {
            _bTM1616_WriteData(pdrv, DIG2,
                               led_flush_buffer.map_smg_data[NUM_TO_3(led_flush_buffer.num)]);
            _bTM1616_WriteData(pdrv, DIG1,
                               led_flush_buffer.map_smg_data[NUM_TO_2(led_flush_buffer.num)]);
        }
        else
        {
            _bTM1616_WriteData(pdrv, DIG2,
                               led_flush_buffer.map_letter_data[led_flush_buffer.letter1]);
            _bTM1616_WriteData(pdrv, DIG1,
                               led_flush_buffer.map_letter_data[led_flush_buffer.letter2]);
        }
    }
    else
    {
        clrbit(led_flush_buffer.icon, led_flush_buffer.map_icon[LED_TEMP_UNIT_ICON]);
        if (led_flush_buffer.type == 0)
        {
            _bTM1616_WriteData(pdrv, DIG1,
                               led_flush_buffer.map_smg_data[NUM_TO_3(led_flush_buffer.num)]);
            _bTM1616_WriteData(pdrv, DIG2,
                               led_flush_buffer.map_smg_data[NUM_TO_2(led_flush_buffer.num)]);
        }
        else
        {
            _bTM1616_WriteData(pdrv, DIG1,
                               led_flush_buffer.map_letter_data[led_flush_buffer.letter1]);
            _bTM1616_WriteData(pdrv, DIG2,
                               led_flush_buffer.map_letter_data[led_flush_buffer.letter2]);
        }
    }

    _bTM1616_WriteData(pdrv, DIG3, led_flush_buffer.icon);
    _bTM1616_WriteData(pdrv, DIG4, led_flush_buffer.icon >> 8);
}
static void _led_show_icon(bDriverInterface_t *pdrv, uint16_t icon)
{
    led_flush_buffer.icon = icon;
    bDRIVER_GET_HALIF(_if, bTM1616_HalIf_t, pdrv);
    _bTM1616_WriteData(pdrv, DIG3, icon);
    _bTM1616_WriteData(pdrv, DIG4, icon >> 8);
}
static void _led_show_num(bDriverInterface_t *pdrv, uint16_t num)
{
    bDRIVER_GET_HALIF(_if, bTM1616_HalIf_t, pdrv);
    if (_if->direction == 0)
    {
        setbit(led_flush_buffer.icon, led_flush_buffer.map_icon[LED_TEMP_UNIT_ICON]);
        clrbit(led_flush_buffer.icon, led_flush_buffer.map_icon[LED_TEMP_UNIT_ICON_R]);
        clrbit(led_flush_buffer.icon, led_flush_buffer.map_icon[LED_HALF_NUM_ICON_R]);
        if (NUM_TO_1(num) != 0)
        {
            setbit(led_flush_buffer.icon, led_flush_buffer.map_icon[LED_HALF_NUM_ICON]);
            setbit(led_flush_buffer.icon, led_flush_buffer.map_icon[lED_DECIMAPOINT_ICON]);
        }
        else
        {
            clrbit(led_flush_buffer.icon, led_flush_buffer.map_icon[LED_HALF_NUM_ICON]);
            clrbit(led_flush_buffer.icon, led_flush_buffer.map_icon[lED_DECIMAPOINT_ICON]);
        }
    }
    else
    {
        setbit(led_flush_buffer.icon, led_flush_buffer.map_icon[LED_TEMP_UNIT_ICON_R]);
        clrbit(led_flush_buffer.icon, led_flush_buffer.map_icon[LED_TEMP_UNIT_ICON]);
        clrbit(led_flush_buffer.icon, led_flush_buffer.map_icon[LED_HALF_NUM_ICON]);
        if (NUM_TO_1(num) != 0)
        {
            setbit(led_flush_buffer.icon, led_flush_buffer.map_icon[LED_HALF_NUM_ICON_R]);
            setbit(led_flush_buffer.icon, led_flush_buffer.map_icon[lED_DECIMAPOINT_ICON_R]);
        }
        else
        {
            clrbit(led_flush_buffer.icon, led_flush_buffer.map_icon[LED_HALF_NUM_ICON_R]);
            clrbit(led_flush_buffer.icon, led_flush_buffer.map_icon[lED_DECIMAPOINT_ICON_R]);
        }
    }

    led_flush_buffer.type = 0;
    led_flush_buffer.num  = num;
}

// 显示字母
// void _led_show_letter(bDriverInterface_t *pdrv, uint8_t pos, uint8_t letter)
// {
//     bDRIVER_GET_HALIF(_if, bTM1616_HalIf_t, pdrv);
//     led_flush_buffer.type = 0;

//     uint8_t dig           = pos == 0 ? DIG1 : DIG2;
//     if (_if->direction == 0)
//     {
//         _bTM1616_WriteData(pdrv, dig, led_flush_buffer.map_letter_data[letter]);
//     }
//     else
//     {

//         _bTM1616_WriteData(pdrv, dig, led_flush_buffer.map_letter_data[letter]);
//     }
// }
void _led_clear_unit(bDriverInterface_t *pdrv)
{
    bDRIVER_GET_HALIF(_if, bTM1616_HalIf_t, pdrv);
    clrbit(led_flush_buffer.icon, led_flush_buffer.map_icon[LED_HALF_NUM_ICON]);
    clrbit(led_flush_buffer.icon, led_flush_buffer.map_icon[lED_DECIMAPOINT_ICON]);
    clrbit(led_flush_buffer.icon, led_flush_buffer.map_icon[LED_TEMP_UNIT_ICON]);
    clrbit(led_flush_buffer.icon, led_flush_buffer.map_icon[LED_HALF_NUM_ICON_R]);
    clrbit(led_flush_buffer.icon, led_flush_buffer.map_icon[lED_DECIMAPOINT_ICON_R]);
    clrbit(led_flush_buffer.icon, led_flush_buffer.map_icon[LED_TEMP_UNIT_ICON_R]);
    // _bTM1616_WriteData(pdrv, DIG3, led_flush_buffer.icon);
    // _bTM1616_WriteData(pdrv, DIG4, led_flush_buffer.icon >> 8);
}
void _led_show_close(bDriverInterface_t *pdrv)
{
    _bTM1616_WriteData(pdrv, DIG2, 0);
    _bTM1616_WriteData(pdrv, DIG1, 0);
    _bTM1616_WriteData(pdrv, DIG3, 0);
    _bTM1616_WriteData(pdrv, DIG4, 0);
    _bTM1616_WriteCmd(pdrv, DISPLAY_OFF);
}
// 辅助函数，将字符映射到相应的LED字母标识符
static int mapCharToLetter(char c)
{
    switch (c)
    {
        case 'A':
            return LED_LETTER_A;
        case 'C':
            return LED_LETTER_C;
        case 'L':
            return LED_LETTER_L;
        case 'O':
            return LED_LETTER_O;
        case 'P':
            return LED_LETTER_P;
        case 'F':
            return LED_LETTER_F;
        case 'D':
            return LED_LETTER_D;
        case 'I':
            return LED_LETTER_I;
        case 'N':
            return LED_LETTER_N;
        case 'E':
            return LED_LETTER_E;
        case '2':
            return LED_LETTER_2;
        case 'Z':
            return LED_LETTER_CLOSE;  // 假设'Z'代表'CLOSE'，因为列表中没有'CLOSE'
        default:
            return LED_LETTER_UNKNOWN;  // 返回未知标识符
    }
}
static int _bTM1616Write(bDriverInterface_t *pdrv, uint32_t addr, uint8_t *pbuf, uint32_t len)
{

    if (addr == 0)
    {
        int16_t data = *((uint16_t *)pbuf);
        if (data < 0)
        {
            data = 0;
        }
        if (data > 990)
        {
            data = 990;
        }
        _led_show_num(pdrv, data);
        led_flush_buffer.num = data;
    }
    else if (addr == 1)
    {
        // b_log("bTM1616_Write pbuf %s\n", pbuf);
        led_flush_buffer.type = 1;
        // 处理每个字符，确保访问不会越界
        uint8_t letter = mapCharToLetter(pbuf[0]);
        if (letter != LED_LETTER_UNKNOWN)
        {

            led_flush_buffer.letter1 = letter;
        }

        letter = mapCharToLetter(pbuf[1]);
        if (letter != LED_LETTER_UNKNOWN)
        {
            led_flush_buffer.letter2 = letter;
        }
        _led_clear_unit(pdrv);
    }
    else if (addr == 2)
    {
        uint16_t data = *((uint16_t *)pbuf);
        //  b_log("bTM1616_Write pbuf %d\n", data);
        _led_show_num(pdrv, data);
        led_flush_buffer.num = data;
        _led_clear_unit(pdrv);
    }
    _led_flush(pdrv);
    return len;
}

static int _bTM1616Ctl(bDriverInterface_t *pdrv, uint8_t cmd, void *param)
{
    bLEDDrvIcon_t get_cmd    = (bLEDDrvIcon_t)cmd;
    uint8_t       onoff      = *(uint8_t *)param;
    uint8_t       brightness = *(uint8_t *)param;
    uint8_t       direction  = *(uint8_t *)param;
    bDRIVER_GET_HALIF(_if, bTM1616_HalIf_t, pdrv);
    switch (get_cmd)
    {
        case CMD_AUTO_MODE_ICON:
            onoff ? setbit(led_flush_buffer.icon, led_flush_buffer.map_icon[LED_AUTO_MODE_ICON])
                  : clrbit(led_flush_buffer.icon, led_flush_buffer.map_icon[LED_AUTO_MODE_ICON]);

            break;
        case CMD_MANUAL_MODE_ICON:
            onoff ? setbit(led_flush_buffer.icon, led_flush_buffer.map_icon[LED_MANUAL_MODE_ICON])
                  : clrbit(led_flush_buffer.icon, led_flush_buffer.map_icon[LED_MANUAL_MODE_ICON]);
            //  _led_show_icon(pdrv, led_flush_buffer.icon);
            break;
        case CMD_HOLIDAY_ICON:
            onoff ? setbit(led_flush_buffer.icon, led_flush_buffer.map_icon[LED_HOLIDAY_ICON])
                  : clrbit(led_flush_buffer.icon, led_flush_buffer.map_icon[LED_HOLIDAY_ICON]);

            break;
        case CMD_CL_ICON:

            onoff ? setbit(led_flush_buffer.icon, led_flush_buffer.map_icon[LED_CL_ICON])
                  : clrbit(led_flush_buffer.icon, led_flush_buffer.map_icon[LED_CL_ICON]);

            break;
        case CMD_HOT_ICON:
            onoff ? setbit(led_flush_buffer.icon, led_flush_buffer.map_icon[LED_HOT_ICON])
                  : clrbit(led_flush_buffer.icon, led_flush_buffer.map_icon[LED_HOT_ICON]);

            break;
        case CMD_ECO_ICON:
            onoff ? setbit(led_flush_buffer.icon, led_flush_buffer.map_icon[LED_ECO_ICON])
                  : clrbit(led_flush_buffer.icon, led_flush_buffer.map_icon[LED_ECO_ICON]);

            break;
        case CMD_WIFI_ICON:
            onoff ? setbit(led_flush_buffer.icon, led_flush_buffer.map_icon[LED_WIFI_ICON])
                  : clrbit(led_flush_buffer.icon, led_flush_buffer.map_icon[LED_WIFI_ICON]);

            break;
        case CMD_LOW_BATTERY_ICON:
            onoff ? setbit(led_flush_buffer.icon, led_flush_buffer.map_icon[LED_LOW_BATTERY_ICON])
                  : clrbit(led_flush_buffer.icon, led_flush_buffer.map_icon[LED_LOW_BATTERY_ICON]);

            break;
        case CMD_SET_BRIGHTNESS:
            if (brightness > 0 && brightness < 9)
            {
                _if->brightness = brightness;
                _bTM1616_WriteCmd(pdrv,
                                  DISPLAY_ON | (SMG_Light1 - 1 + brightness));  // 数码管亮度1
            }
            break;
        case CMD_SET_DIRECTION:

            _if->direction = direction;
            if (direction == 0)
            {
                led_flush_buffer.map_smg_data    = TM1616_SMG;
                led_flush_buffer.map_letter_data = TM1616_LED_LETTER;
            }
            else
            {
                led_flush_buffer.map_smg_data    = TM1616_SMG_R;
                led_flush_buffer.map_letter_data = TM1616_LED_LETTER_R;
            }
            _led_flush(pdrv);
            break;
        default:
            break;
    }
    if (cmd == CMD_DISPALY_ALL)
    {
        _bTM1616_WriteCmd(pdrv, DISPLAY_ON | (SMG_Light8));
        _bTM1616_WriteData(pdrv, DIG1, 0xFF);
        _bTM1616_WriteData(pdrv, DIG2, 0xFF);
        _bTM1616_WriteData(pdrv, DIG3, 0xFF);
        _bTM1616_WriteData(pdrv, DIG4, 0xFF);
    }
    else if (cmd == CMD_CLOSE)
    {
        led_flush_buffer.is_close = true;
        _led_show_close(pdrv);
    }
    else if (cmd == CMD_OPEN)
    {
        led_flush_buffer.is_close = false;
        bTM1616_Init(pdrv);
        _led_flush(pdrv);
    }
    else if (cmd == CMD_FLUSH)
    {
        _led_flush(pdrv);
    }
    else if (cmd == CMD_GET_STATUS)
    {
        *(uint8_t *)param = led_flush_buffer.is_close;
    }
    else
    {
        if (led_flush_buffer.is_close == false)
            _led_flush(pdrv);
    }
}

void tm1616_test_case(bDriverInterface_t *pdrv)
{
    _bTM1616Write(pdrv, 1, "OP", 2);
    bHalDelayMs(1000);
    _bTM1616Write(pdrv, 1, "AD", 2);
    bHalDelayMs(1000);
    _bTM1616Write(pdrv, 1, "LA", 2);
    bHalDelayMs(1000);
    _bTM1616Write(pdrv, 1, "IN", 2);
    bHalDelayMs(1000);
}
/**
 * \}
 */

/**
 * \addtogroup TM1616_Exported_Functions
 * \{
 */
int bTM1616_Init(bDriverInterface_t *pdrv)
{
    b_log("bTm1616 init\n");
    bDRIVER_STRUCT_INIT(pdrv, DRIVER_NAME, bTM1616_Init);
    bDRIVER_GET_HALIF(_if, bTM1616_HalIf_t, pdrv);
    pdrv->ctl   = _bTM1616Ctl;
    pdrv->write = _bTM1616Write;

    if (_if->direction == 0)
    {
        led_flush_buffer.map_smg_data    = TM1616_SMG;
        led_flush_buffer.map_letter_data = TM1616_LED_LETTER;
        led_flush_buffer.map_icon        = TM1616_ICON;
    }
    else
    {
        led_flush_buffer.map_smg_data    = TM1616_SMG_R;
        led_flush_buffer.map_letter_data = TM1616_LED_LETTER_R;
        led_flush_buffer.map_icon        = TM1616_ICON;
    }

    bMcuGpioConfig(B_HAL_GPIOA, B_HAL_PIN11, B_HAL_GPIO_OUTPUT, B_HAL_GPIO_PULLUP);  // open
    bMcuGpioWritePin(B_HAL_GPIOA, B_HAL_PIN11, 1);
    bMcuGpioConfig(_if->clk.port, _if->clk.pin, B_HAL_GPIO_OUTPUT, B_HAL_GPIO_NOPULL);
    bMcuGpioConfig(_if->stb.port, _if->stb.pin, B_HAL_GPIO_OUTPUT, B_HAL_GPIO_NOPULL);
    bMcuGpioConfig(_if->din.port, _if->din.pin, B_HAL_GPIO_OUTPUT, B_HAL_GPIO_NOPULL);

    _bTM1616_WriteCmd(pdrv, DIGIT4_SEGMENT7);  // 数据显示模式：4位7段
    _bTM1616_WriteCmd(pdrv, FIXED_ADD);        // 自动地址增

    _bTM1616_WriteData(pdrv, DIG1, 0);
    _bTM1616_WriteData(pdrv, DIG2, 0);
    _bTM1616_WriteData(pdrv, DIG3, 0);
    _bTM1616_WriteData(pdrv, DIG4, 0);

    if (_if->brightness < 9 && _if->brightness > 0)
        _bTM1616_WriteCmd(
            pdrv, DISPLAY_ON | (SMG_Light1 - 1 + _if->brightness));  // 数码管亮度1//设置错误不会亮
    else
    {
        _bTM1616_WriteCmd(pdrv, DISPLAY_ON | (SMG_Light1));  // 数码管亮度1
    }

    // tm1616_test_case(pdrv);
    return 0;
}

#ifdef BSECTION_NEED_PRAGMA
#pragma section driver_init
#endif
bDRIVER_REG_INIT(B_DRIVER_TM1616, bTM1616_Init);
#ifdef BSECTION_NEED_PRAGMA
#pragma section
#endif
#include "b_os.h"
/*
 * @brief
 */
static bool led_is_idle(void)
{
    return led_flush_buffer.is_close;
}

bPM_REG_INSTANCE("pm_led", led_is_idle, NULL, NULL);
/**
 * \}
 */

/**
 * \}
 */

/**
 * \}
 */
/************************ Copyright (c) 2019 Bean *****END OF FILE****/
